package com.hs.order.controller;


import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import com.hs.order.pojo.UnOrderInfo;
import com.hs.order.service.IUnOrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bai
 * @since 2022-07-25
 */
@RestController
@RequestMapping("/un-order-info")
@Api(tags = "异常订单处理模块")
public class UnOrderInfoController {

    @Autowired
    private IUnOrderInfoService unOrderInfoService;

    @PostMapping("/create")
    @ApiOperation("创建异常订单处理信息")
    public ResultVo createUnOrderInfo(@RequestBody UnOrderInfo unOrderInfo){

        return unOrderInfoService.save(unOrderInfo) ? new ResultVo(ResultCode.SUCCESS) : new ResultVo(ResultCode.FAILED);

    }

    @PostMapping("/imageUpload.do")
    @ApiOperation("反馈图片上传")
    public Map<String,Object> imageUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        String fileSavePath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println("fileSavePath = " + fileSavePath);
        if (null == multipartFile || multipartFile.getSize() <= 0) {
            return new HashMap<String,Object>(){{put("code",400);put("msg","请选择上传文件。");}};
        }
        //文件名
        String originalName = multipartFile.getOriginalFilename();
        String fileName= UUID.randomUUID().toString().replace("-", "");
        String picNewName = fileName + originalName.substring(originalName.lastIndexOf("."));
        String imgRealPath = fileSavePath + picNewName;
        try {
            //保存图片-将multipartFile对象装入image文件中
            File imageFile=new File(imgRealPath);
            multipartFile.transferTo(imageFile);
        } catch (Exception e) {
            return new HashMap<String,Object>(){{put("code",400);put("msg","图片保存异常:"+e);}};
        }
        return new HashMap<String,Object>(){{put("code",200);put("msg",picNewName);}};
    }

}

